home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / macgzip_03b2-src / macos / Posix / ThinkCPosix Sources / stat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-18  |  3.2 KB  |  170 lines  |  [TEXT/MPS ]

  1. /* $Id: $ */
  2.  
  3. /*
  4.  * Minimal 'stat' emulation: tells directories from files and
  5.  * gives length and mtime.
  6.  *
  7.  * Largely based on code written by Guido van Rossum, CWI, Amsterdam
  8.  * and placed by him in the public domain --
  9.  * retrieved by anonymous FTP from ftp.cwi.nl
  10.  */
  11. /*
  12.  * Original from ThinkCPosix by Timothy Murphy <tim@maths.tcd.ie>,
  13.  * Trinity College Dublin
  14.  *
  15.  * Modified by SPDsoft <macspd@ivo.cps.unizar.es> to use Unix dates
  16.  * seconds from Mac 'Fri Jan  1 00:00:00 1904' to
  17.  * Un*x 'Thu Jan  1 00:00:00 1970':                    2082844800
  18.  */
  19.  
  20. #define    U2MSEC    2082844800
  21.  
  22.  
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include <fcntl.h>
  27.  
  28. /*
  29.  * Stupid CodeWarrior 1.0a1s does not defines these
  30.  */
  31.  
  32. #ifndef EACCES
  33. #define EACCES        (-54)
  34. #endif
  35. #ifndef ENOENT
  36. #define ENOENT        (-43)
  37. #endif
  38.  
  39. FILE *fdopen(int, char *); /* I don't want to include unix.h */
  40.  
  41.  
  42. int sys_nerr = 0;
  43. char *sys_errlist[] = {""};
  44. char *myenviron[] = {NULL};
  45. char **environ = myenviron;
  46. extern int __uid, __gid;
  47. int Stat(char*, long, struct stat*);
  48.  
  49.  
  50. /* Bits in ioFlAttrib: */
  51. #define LOCKBIT    (1<<0)        /* File locked */
  52. #define DIRBIT    (1<<4)        /* It's a directory */
  53.  
  54. /*
  55.  * Mac-ky "stat" in which filename is given relative to a directory,
  56.  * specified by long DirID.
  57.  */
  58.  
  59. int
  60. Stat(name, DirID, buf)
  61.     char *name;
  62.     long DirID;
  63.     struct stat *buf;
  64. {
  65.     CInfoPBRec cipbr;
  66.     HFileInfo *fpb = (HFileInfo*)&cipbr;
  67.     DirInfo *dpb = (DirInfo*)&cipbr;
  68.     unsigned char pname[256];
  69.     short err;
  70.  
  71.     strcpy((char*)pname, name);
  72.     c2pstr((char*)pname);
  73.  
  74.     dpb->ioDrDirID = DirID;
  75.     fpb->ioNamePtr = pname;
  76.     fpb->ioVRefNum = 0;
  77.     fpb->ioFDirIndex = 0;
  78.     fpb->ioFVersNum = 0;
  79.     err = PBGetCatInfo(&cipbr, FALSE);
  80.     if (err != noErr) {
  81.         errno = ENOENT;
  82.         return -1;
  83.     }
  84.     if (fpb->ioFlAttrib & LOCKBIT)
  85.         buf->st_mode= 0444;
  86.     else
  87.         buf->st_mode= 0666;
  88.     if (fpb->ioFlAttrib & DIRBIT) {
  89.         buf->st_mode |= 0111 | S_IFDIR;
  90.         buf->st_size= dpb->ioDrNmFls;
  91.         buf->st_rsize= 0;
  92.     }
  93.     else {
  94.         buf->st_mode |= S_IFREG;
  95.         if (fpb->ioFlFndrInfo.fdType == 'APPL')
  96.             buf->st_mode |= 0111;
  97.         buf->st_size= fpb->ioFlLgLen;
  98.         buf->st_rsize= fpb->ioFlRLgLen;
  99.     }
  100.     buf->st_atime= fpb->ioFlCrDat - U2MSEC;
  101.     buf->st_mtime= fpb->ioFlMdDat - U2MSEC;
  102.     buf->st_ctime= fpb->ioFlCrDat - U2MSEC;
  103.     buf->st_ino= (unsigned short)fpb->ioDirID;
  104.     buf->st_uid= __uid;
  105.     buf->st_gid= __gid;
  106.     buf->st_FlFndrInfo= fpb->ioFlFndrInfo;
  107.     return 0;
  108. }
  109.  
  110. int
  111. stat(path, buf)
  112.     char *path;
  113.     struct stat *buf;
  114. {
  115.     return Stat(path, 0L, buf);
  116. }
  117.  
  118. int
  119. fstat(fd, buf)
  120.     int fd;
  121.     struct stat *buf;
  122. {
  123. #if defined(THINK_C)
  124.     FCBPBRec fcb;
  125.     unsigned char pname[256];
  126.     long DirID;
  127.     short err;
  128.     
  129.     /*
  130.      * fdopen() gives FILE entry with name of file,
  131.      * as well as RefNum of containing directory
  132.      */
  133.     
  134.     FILE *fp = fdopen(fd, "");
  135.     
  136.     /* 
  137.      * PBGetFCBInfo() converts short RefNum to long DirID
  138.      */
  139.      
  140.     fcb.ioRefNum= fp->refnum;
  141.     fcb.ioVRefNum= 0;
  142.     fcb.ioFCBIndx= 0;
  143.     fcb.ioNamePtr= pname;
  144.     err= PBGetFCBInfo(&fcb, FALSE);
  145.     if (err != noErr) {
  146.         errno = ENOENT;
  147.         return -1;
  148.     }
  149.     DirID = fcb.ioFCBParID;
  150.     
  151.     p2cstr(pname);
  152.     return Stat((char*)pname, DirID, buf);
  153.  
  154. #else
  155. # if !defined(__MWERKS__)
  156.     int err;
  157.     char fname[256];
  158.     
  159.     err = ioctl( fd, FIOFNAME, (long *)fname );
  160.     if( err == 0 ) return Stat( fname, 0L, statPtr );
  161.     else err = -1;
  162.     
  163.     return err;
  164. # else
  165.     return -1;
  166. # endif
  167. #endif
  168. }
  169.  
  170.